package com.hyt.it.ogt.ks.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyt.it.ogt.ks.mapper.CandidateAnswerMapper;
import com.hyt.it.ogt.ks.mapper.CandidatePaperMapper;
import com.hyt.it.ogt.ks.model.entity.CandidatePaper;
import com.hyt.it.ogt.ks.model.entity.CandidateTimeLength;
import com.hyt.it.ogt.ks.model.vo.CandidateFinishExamVO;
import com.hyt.it.ogt.ks.service.IOverExamService;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class OverExamServiceImpl implements IOverExamService {
    @Resource
    private CandidateAnswerMapper candidateAnswerMapper;
    @Resource
    private CandidatePaperMapper candidatePaperMapper;

    public void a() {

    }

    private void updateAllPaperScore(String examId, String candidateId) {
        List<CandidatePaper> candidatePapers = candidatePaperMapper.selectList(Wrappers.<CandidatePaper>lambdaQuery()
                .select(CandidatePaper::getId, CandidatePaper::getPaperId, CandidatePaper::getPaperVersion,
                        CandidatePaper::getExamUseTime, CandidatePaper::getAccessTime,
                        CandidatePaper::getRemainingTime, CandidatePaper::getExtraTime)
                .eq(CandidatePaper::getExamId, examId).eq(CandidatePaper::getCandidateId, candidateId));
        candidatePapers.stream().forEach(item -> {
            BigDecimal objectiveScore = candidateAnswerMapper.getObjectiveScoreByPaper(examId, candidateId,
                    item.getPaperId(), item.getPaperVersion());
            if (objectiveScore == null) {
                objectiveScore = new BigDecimal(0);
            }
            CandidatePaper candidatePaper = new CandidatePaper();
            candidatePaper.setId(item.getId());
            candidatePaper.setObjectiveScore(objectiveScore);
            candidatePaper.setFinish(true);
            long currentTimeMillis = System.currentTimeMillis();
            if (item.getAccessTime() == null) {
                //首次访问 AccessTime可能为空
                candidatePaper.setAccessTime(currentTimeMillis);
                item.setAccessTime(currentTimeMillis);
            }
            if (null != item.getExamUseTime() && null != item.getAccessTime()) {
                // 计算考生实际耗时
                CandidateTimeLength tl = new CandidateTimeLength();
                tl.setExamId(examId);
                tl.setExtraTime(item.getExtraTime());
                tl.setRemainingTime(item.getRemainingTime());
                tl.setAccessTime(item.getAccessTime());
                // 更新剩余时长
            }
            candidatePaperMapper.updateById(candidatePaper);
        });
    }

    public BigDecimal calScore(CandidateFinishExamVO param) {
        candidateAnswerMapper.calcObjectiveScore(param);
        //获取本次客观题考试总成绩
        BigDecimal objectiveScore = candidateAnswerMapper.getObjectiveScore(param);
        if (objectiveScore == null) {
            objectiveScore = new BigDecimal(0);
        }
        return objectiveScore;
    }
}
